//
//  LOLFuwenbrowserViewController.m
//  LOLXXbox
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLFuwenbrowserViewController.h"
#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
#define PADDING5 WIDTH/75
#import "LOLfuwenGCollectionViewCell.h"
#import <UIImageView+WebCache.h>
@interface LOLFuwenbrowserViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UIButton *backButton;
@property(nonatomic,retain)UILabel *detailLabel;
@property(nonatomic,retain)UIButton *saveButton;
@property(nonatomic,retain)UICollectionView *skinCollectionV;
@property(nonatomic,retain)UIView *topView;
@property(nonatomic,retain)UIView *bottomView;
@property(nonatomic,assign)BOOL isHidden;
@property(nonatomic,retain)UIImage *skinImage;
@end

@implementation LOLFuwenbrowserViewController
-(BOOL)prefersStatusBarHidden
{
    
    return YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     [self createCollectionV];
}

-(void)createCollectionV
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHiddenType) name:@"hiddenView" object:nil];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.skinCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];

    self.skinCollectionV.backgroundColor = [UIColor blackColor];
    [self.skinCollectionV registerClass:[LOLfuwenGCollectionViewCell class] forCellWithReuseIdentifier:@"fuwenReuse"];
    self.skinCollectionV.delegate = self;
    self.skinCollectionV.dataSource = self;
    self.skinCollectionV.pagingEnabled = YES;
    self.skinCollectionV.allowsSelection = NO;
    
    [self.view addSubview:self.skinCollectionV];

    
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/17)];
    [self.view addSubview:self.topView];

    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-HEIGHT/8, WIDTH, HEIGHT/8)];
    [self.view addSubview:self.bottomView];

    
    self.topView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.650];
    self.bottomView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.650];
    
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(5, 10, WIDTH/17, WIDTH/17);
    [self.backButton addTarget:self action:@selector(dismissVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [self.topView addSubview:self.backButton];
    
    
    
    
    
    
    
    self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, WIDTH-10, 40)];
    self.detailLabel.font = [UIFont boldSystemFontOfSize:18];
    self.detailLabel.textColor = [UIColor whiteColor];
    self.detailLabel.text = self.fuwenModel.fuDes;
    [self.bottomView addSubview:self.detailLabel];

    
    
    self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveButton.frame = CGRectMake(PADDING5, 45, WIDTH/15, WIDTH/15);
    [self.saveButton addTarget:self action:@selector(saveToPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.saveButton setImage:[UIImage imageNamed:@"download"] forState:UIControlStateNormal];
    [self.bottomView addSubview:self.saveButton];
    
    
    
    
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 2;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LOLfuwenGCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"fuwenReuse" forIndexPath:indexPath];
    NSString *url = @"";
    switch (indexPath.row) {
        case 0:
            url = self.fuwenModel.fuPic;
            break;
        case 1:
            url = self.fuwenModel.tianPic;
        default:
            break;
    }
    [cell.skinImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [cell.scrollView setZoomScale:1.0];
        cell.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
        self.skinImage = image;
        CGFloat height=WIDTH*image.size.height/image.size.width;
        cell.skinImageView.bounds=CGRectMake(0, 0, WIDTH, height);
        cell.skinImageView.center = cell.scrollView.center;
        
        cell.isImageAppear = YES;
        
    }];
    
    return  cell;
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.saveButton.userInteractionEnabled = YES;
    
    switch ((NSInteger)(scrollView.contentOffset.x/WIDTH)) {
        case 0:
            self.detailLabel.text = self.fuwenModel.fuDes;
            break;
        case 1:
            self.detailLabel.text = self.fuwenModel.tianDes;
            break;
            
        default:
            break;
    }
    
    
    
    
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.saveButton.userInteractionEnabled = NO;
    
    
}

-(void)changeHiddenType
{
    if(self.isHidden)
    {
        self.topView.hidden =YES;
        self.bottomView.hidden = YES;
        
        
    }
    else
    {
        self.topView.hidden =NO;
        self.bottomView.hidden = NO;
        
    }
    
    self.isHidden = !self.isHidden;
    
    
    
    
}
-(void)dismissVC:(UIButton *)button
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)saveToPhoto:(UIButton *)button
{
    
    //    YMH_fuwenGiftCCell *cell = self.skinCollectionV.visibleCells[0];
    UIImageWriteToSavedPhotosAlbum(self.skinImage, nil, nil, nil);
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
